package com.ktjy.controller;


import com.ktjy.pojo.SysUser;
import com.ktjy.service.SysUserService;
import com.ktjy.utils.Constants;
import com.ktjy.utils.Page;
import jakarta.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.*;

@RestController
@RequestMapping("/api/admin/user")
public class SysUserController {
    Logger log= LoggerFactory.getLogger(SysUserController.class);

    @Autowired
    SysUserService sysUserService;

    @RequestMapping("/page")
    public Map<String,Object> page(SysUser condition,
                       @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                       @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        Map<String,Object> map=new HashMap<>();
        try {
            //查询数据
            Page<SysUser> page=sysUserService.findPage(condition,pageNo,pageSize);
            //响应数据给页面
            map.put("code",1);
            map.put("msg","查询用户信息成功");
            map.put("data",page);
        } catch (Exception e) {
            log.error("SysUserController中page方法执行报错的。",e);
            map.put("code",0);
            map.put("msg","查询用户信息失败");
        }
        return map;
    }

    @PostMapping("/save")
    public Map<String,Object> save(SysUser sysUser, HttpSession session){
        Map<String,Object> map=new HashMap<>();
        try {
            boolean ok=false;
            //获取当前登录用户信息
            SysUser loginUser=(SysUser)session.getAttribute(Constants.SESSION_USER);
            if(sysUser.getId()==null){//做新增
                sysUser.setCreatedUserId(loginUser.getId());
                sysUser.setCreatedTime(new Date());
                ok=sysUserService.save(sysUser);
            }else{//做修改
                sysUser.setUpdatedUserId(loginUser.getId());
                sysUser.setUpdatedTime(new Date());
                ok=sysUserService.update(sysUser);
            }
            if(ok){
                map.put("code",1);
                map.put("msg","保存成功");
            }else{
                map.put("code",0);
                map.put("msg","保存失败");
            }
        } catch (Exception e) {
            log.error("SysUserController中save方法执行报错的。",e);
            map.put("code",0);
            map.put("msg","保存失败");
        }
        return map;
    }

    @GetMapping("/view/{id}")
    public Map<String,Object> view(@PathVariable("id") Long id){
        //查询用户详情
        Map<String,Object> map=new HashMap<>();
        try {
            SysUser sysUser=sysUserService.findById(id);
            sysUser.setRole(null);
            if(sysUser!=null){
                map.put("code",1);
                map.put("msg","查询用户详情成功");
                map.put("data",sysUser);
            }else{
                map.put("code",0);
                map.put("msg","查询用户详情失败");
            }
        } catch (Exception e) {
            log.error("SysUserController中view方法执行报错的。",e);
            map.put("code",0);
            map.put("msg","查询用户详情失败");
        }
        return map;
    }

    @DeleteMapping("/del/{id}")
    @ResponseBody
    public Map<String,Object> del(@PathVariable("id") Long id){
        Map<String,Object> map=new HashMap<>();
        try {
            boolean ok= sysUserService.delete(id);
            if(ok){//删除成功
                map.put("code",1);
                map.put("msg","删除成功");
            }else{//删除失败
                map.put("code",0);
                map.put("msg","删除失败");
            }
        } catch (Exception e) {
            log.error("SysUserController中del方法执行报错的。",e);
            map.put("code",0);
            map.put("msg","删除失败");
        }
        return map;
    }

    @GetMapping("/accountExist")
    public Map<String,Object> accountExist(@RequestParam("account") String account){
        Map<String,Object> map=new HashMap<>();
        try {
            SysUser sysUser = sysUserService.findByAccount(account);
            if(sysUser==null){//数据库不存在该账号
                map.put("code",1);
                map.put("msg","该账号可以使用");
            }else{//数据库存在该账号
                map.put("code",0);
                map.put("msg","该用户账号已存在");
            }
        } catch (Exception e) {
            log.error("SysUserController中accountExist方法执行报错的。",e);
            map.put("code",0);
            map.put("msg","调用accountExist接口异常");
        }
        return map;
    }
}
